
package com.yuan.servlet;

import com.yuan.control.CheckControl;
import com.yuan.hibernate.HibernateSessionFactory;
import com.yuan.hibernate.员工信息表;
import com.yuan.hibernate.员工信息表DAO;
import com.yuan.hibernate.考勤表;
import com.yuan.hibernate.考勤表DAO;
import com.yuan.hibernate.考勤表Id;
import com.yuan.prefence.PreferenceKeys;
import com.yuan.util.JsonUtils;
import com.yuan.util.TimeUtil;

import org.hibernate.Transaction;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class checkinservlet extends BaseHttpServlet {
    private 考勤表DAO m考勤表DAO = null;
    private 考勤表Id m考勤表Id = null;
    private 考勤表 m考勤表 = null;
    private 员工信息表 m员工信息表 = null;
    private 员工信息表DAO m员工信息表DAO = null;

    @SuppressWarnings({
            "rawtypes", "unchecked"
    })
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        super.doGet(request, response);
        int userid = -1;
        String m备注 = "";
        String m标示 = "";
        String m位置 = "";
        Double m经度 = 0.0;
        Double m纬度 = 0.0;
        String checktime = "";
        try
        {
            userid = Integer.parseInt(request.getParameter("userid"));
            m备注 = new String(request.getParameter("remark").getBytes("ISO-8859-1"),
                    "UTF-8");
            m位置 = new String(request.getParameter("location").getBytes("ISO-8859-1"),
                    "UTF-8");
            m标示 = new String(request.getParameter("mark").getBytes("ISO-8859-1"),
                    "UTF-8");
            m纬度 = Double.parseDouble(new String(request.getParameter("lat")
                    .getBytes("ISO-8859-1"),
                    "UTF-8"));
            m经度 = Double.parseDouble(new String(request.getParameter("lng")
                    .getBytes("ISO-8859-1"),
                    "UTF-8"));
            checktime = request.getParameter("checkintime");
        } catch (Exception e) {
            mResponse = "输入异常";
            e.printStackTrace();
        }
        if (!TimeUtil.isCheckRightTime(checktime)) {
            mResponse = "输入异常";
        }
        m考勤表DAO = new 考勤表DAO();
        m员工信息表DAO = new 员工信息表DAO();
        m员工信息表 = m员工信息表DAO.findById(userid);
        List<考勤表> tempList = m考勤表DAO.findBy职工姓名(m员工信息表.get姓名());
        for (考勤表 temp : tempList) {
            if (temp.getId().get日期().startsWith(checktime.substring(0, 10))
                    && temp.get备注().contains(PreferenceKeys.CHECKINTIME))
            {
                List<考勤表> list = new ArrayList<考勤表>();
                list.add(temp);
                mResponse = JsonUtils.getFromList(list);
                mResponse = mResponse.replace("{\"id\":", "");
                mResponse = mResponse.replace("},\"位置\"", ",\"位置\"");
                break;
            }
        }
        if (mResponse == null)
        {
            Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
            m考勤表Id = new 考勤表Id();
            m考勤表Id.set序号(userid + "");
            m考勤表Id.set日期(checktime + ".0");
            m考勤表 = new 考勤表();
            m考勤表.setId(m考勤表Id);
            m考勤表.set位置(m位置);
            m考勤表.set出勤标示(m标示);
            Map map = new HashMap();
            map.put("上班是否正常", m备注);
            map.put(PreferenceKeys.CHECKINTIME, checktime);
            List temp1 = new ArrayList();
            temp1.add(map);
            m考勤表.set备注(JsonUtils.getFromList(temp1));
            m考勤表.set所属队室(m员工信息表.get所属队室());
            m考勤表.set职工姓名(m员工信息表.get姓名());
            m考勤表.set纬度(m纬度);
            m考勤表.set经度(m经度);
            m考勤表DAO.save(m考勤表);
            tx.commit();
            CheckControl.getInstance().update月度考勤表(m员工信息表.get姓名(), m员工信息表.get所属队室());
            考勤表 temp = m考勤表DAO.findById(m考勤表Id);
            List<考勤表> list = new ArrayList<考勤表>();
            list.add(temp);
            mResponse = JsonUtils.getFromList(list);
            mResponse = mResponse.replace("{\"id\":", "");
            mResponse = mResponse.replace("},\"位置\"", ",\"位置\"");
        }
        closeAndClear();
    }

    protected synchronized void closeAndClear() {
        m考勤表DAO = null;
        m考勤表Id = null;
        m考勤表 = null;
        m员工信息表 = null;
        m员工信息表DAO = null;
        super.closeAndClear();
    }
}
